package com.skivingcloud.admin.sys.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;
import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.admin.sys.entity.SysFormDesigner;
import com.skivingcloud.admin.sys.service.ISysFormDesignerService;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 表单设计表 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-06-30
 */
@RestController
@RequestMapping("/sys/formDesigner")
public class SysFormDesignerController {

	private static Logger log = LoggerFactory.getLogger(SysFormDesignerController.class);
	
	@Autowired
	private ISysFormDesignerService sysFormDesignerService;

	/**
	 * 分页
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		/*Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", page);
		params.put("limit", limit);
		params.put("dataForm", dataForm);*/
		PageUtils pages = sysFormDesignerService.queryPage(params);
		return R.ok().put("page", pages);
	}

	/**
	 * 新增
	 * @param sysFormDesigner
	 * @return
	 */
	@UserOperateLog(module = "表单管理", methods = "save", description = "新增表单设计")
	@PostMapping("/save")
	public R save(@RequestBody SysFormDesigner sysFormDesigner) {
		try {
			sysFormDesignerService.add(sysFormDesigner);
			return R.ok();
		} catch (Exception e) {
			log.error("保存表单设计失败!", e);
			return R.error("保存表单设计失败!");
		}
	}
	
	/**
	 * 修改
	 * @param sysFormDesigner
	 * @return
	 */
	@UserOperateLog(module = "表单管理", methods = "update", description = "修改表单设计")
	@PostMapping("/update")
	public R update(@RequestBody SysFormDesigner sysFormDesigner) {
		try {
			sysFormDesignerService.update(sysFormDesigner);
			return R.ok();
		} catch (Exception e) {
			log.error("保存表单设计失败!", e);
			return R.error("保存表单设计失败!");
		}
	}

	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@UserOperateLog(module = "表单管理", methods = "delete", description = "删除表单设计")
	@PostMapping("/delete")
	public R delete(@RequestBody String[] ids) {
		try {
			sysFormDesignerService.deleteBatch(ids);
			return R.ok();
		} catch (Exception e) {
			log.error("删除表单设计失败!", e);
			return R.error("删除表单设计失败!");
		}
	}

	/**
	 * 根据编码加载表单设计数据
	 * 
	 * @param formCode
	 * @return
	 */
	@GetMapping("/getFormJsonByCode/{formCode}")
	public R getFormJsonByCode(@PathVariable("formCode") String formCode) {
		try {
			JsonNode formJson = sysFormDesignerService.getFormJsonByCode(formCode);
			return R.ok().put("formJson", formJson);
		} catch (Exception e) {
			log.error("根据编码加载表单设计数据失败!", e);
			return R.error(1, "根据编码加载表单设计数据失败!");
		}
	}

	/**
	 * 根据主键查询
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) throws Exception {
		SysFormDesigner sysFormDesigner = sysFormDesignerService.selectById(id);
		return R.ok().put("data", sysFormDesigner);
	}

	/*
	 * 查询表单设计
	 */
	@GetMapping("/getFormDesignerList")
	public R getFormDesignerList() throws Exception {
		List<SysFormDesigner> list = sysFormDesignerService.getBusinessTypeList();
		return R.ok().put("list", list);
	}
	
}
